SetGfxDriver 1:Graphics 340,200,8,2:SeedRnd MilliSecs() :Viewport 0,0,320,300:SetBuffer FrontBuffer() 
Global Arial,nArial,timer,nums,numz,numx,z,z1,errorflag,frames,iterated,lplen,lptr,lframes,literated
timer = CreateTimer(100) :nums = CreateBank(258):numz= CreateBank(16):numx= CreateBank(16):erroflag=False
For i = 0 To 256 
t = Rand(0,15):PokeByte nums,i, t:PokeByte numz,t, PeekByte (numz,t) +1 
Next 
cnt=0:z=0:z1=1:stopflag=True:stopflag1=False:prestopflag1=False:counter=0:current=0:iterated=0:frames=0

findhuman():findtarget()
While Not KeyHit(1) 
Cls  
;set256257()
stat(iterated,frames)
 movehuman():findhuman():findtarget()

label1()
Flip 
If KeyHit(57) > 0 WaitMouse()
Wend  













Function unentropy()
ptr=0:plen=0:prev=0:cur=0
  If PeekByte (nums,0)>127 prev= 1 Else prev=0
For i=1 To 255
  If PeekByte (nums,i)>127 cur = 1 Else cur=0 
  If (cur=prev) plen=plen+1 
  If ((cur<>prev) And (plen>lplen)) lplen=plen:plen=0:lptr=ptr:ptr=i:prev=cur:lframes=frames:literated=iterated:flag=True
  If (prev<>cur) prev=cur:ptr=i:plen=0
Next
Color 0,255,0:Text 20,170,lptr+" ptr, "+lplen+" plen, "+lframes+" frames, "+literated+" iterations, ",False ,False 
;If flag=True WaitMouse()
End Function


Function movehuman()
current=PeekByte (nums,z)
If z1<=z Then
   For i=z To 254
      PokeByte nums,i,PeekByte (nums,i+1)
   Next
   PokeByte nums,255,PeekByte (nums,0)
   For i=0 To z1-1
      PokeByte nums,i,PeekByte (nums,i+1)
   Next
   frames=frames+1:iterated=0
Else
;   For i=z To z1-1
;      PokeByte nums,i,PeekByte (nums,i+1)
;   Next
;   iterated=iterated+1
   For i=z To z1-1
    If z1<prostir-2 And z>2 Then
          PokeByte nums,i,(PeekByte (nums,i+1) Xor 255) 
        Else
          PokeByte nums,i,PeekByte (nums,i+1)
       EndIf
   Next
   iterated=iterated+1
EndIf
;PokeByte nums,z1,current
If z1<prostir-2 And z>2 Then PokeByte nums,z1,(current Xor 255) Else PokeByte nums,z1,current
z=z1+1
If z=256 Then z=0
End Function


Function findhuman()
For i=z To 255
  If PeekByte (nums,i)>7 Then z=i:i=256;:finded=True Else finded=False 
Next
End Function

Function findtarget()
PokeByte nums,256,PeekByte (nums,0):PokeByte nums,257,PeekByte (nums,1)
current = PeekByte (nums,z)
If PeekByte (nums,z)<PeekByte (nums,z+1) za1 = 0 Else za1=1
If PeekByte (nums,z)<PeekByte (nums,z+2) za2 = 0 Else za2=1
If PeekByte (nums,z+1)<PeekByte (nums,z+2) za3 = 0 Else za3=1
If PeekByte (nums,z)=PeekByte (nums,z+1) za1 = 2
If PeekByte (nums,z)=PeekByte (nums,z+2) za2 = 2
If PeekByte (nums,z+1)=PeekByte (nums,z+2) za3 = 2
For i =z+1 To 255
If PeekByte (nums,i)<PeekByte (nums,i+1) a1 = 0 Else a1=1
If PeekByte (nums,i)<PeekByte (nums,i+2) a2 = 0 Else a2=1
If PeekByte (nums,i+1)<PeekByte (nums,i+2) a3 = 0 Else a3=1
If PeekByte (nums,i)=PeekByte (nums,i+1) a1 = 2
If PeekByte (nums,i)=PeekByte (nums,i+2) a2 = 2
If PeekByte (nums,i+1)=PeekByte (nums,i+2) a3 = 2
If(a1=za1 And a2=za2 And a3=za3) Then z1=i:i=256:finded=True Else finded=False 
Next
If finded=False Then
For i =0 To z
If PeekByte (nums,i)<PeekByte (nums,i+1) a1 = 0 Else a1=1
If PeekByte (nums,i)<PeekByte (nums,i+2) a2 = 0 Else a2=1
If PeekByte (nums,i+1)<PeekByte (nums,i+2) a3 = 0 Else a3=1
If PeekByte (nums,i)=PeekByte (nums,i+1) a1 = 2
If PeekByte (nums,i)=PeekByte (nums,i+2) a2 = 2
If PeekByte (nums,i+1)=PeekByte (nums,i+2) a3 = 2
If(a1=za1 And a2=za2 And a3=za3) Then z1=i:i=256:finded=True Else finded=False 
Next
EndIf
End Function

Function stat(iterated,frames)
Text 20,180,frames+" frames, "+iterated+" iterations, "+z+" pointer",False ,False 
End Function

Function set256257()
PokeByte nums,256, PeekByte (nums,0):PokeByte nums,257, PeekByte (nums,1)
End Function



Function label1()
;Color 255,255,0
;For i = 0 To 15
;   Text 9,18+i*7,PeekByte (numz,i),False ,False
;   PokeByte numx,i,0
;Next
cnt=0
For i1 = 0 To 15
 For i = 0 To 15
    If (PeekByte (nums,i+i1*16)>7 )Then Color 0,255,0 Else Color 0,100,0
    PokeByte numx,PeekByte (nums,i+i1*16), PeekByte (numx,PeekByte (nums,i+i1*16)) +1 
    Text 9+18+i*10,18+i1*7,PeekByte (nums,i+i1*16),False ,False 
        If (i1*16+i=z) Color 255,0,0: Rect 9+18+i*10,18+i1*7,11,7,0
        If (i1*16+i=z1) Color 255,255,0: Rect 9+18+i*10,18+i1*7,11,7,0
    If (PeekByte (nums,cnt)>7 )Then Color 0,255,0 Else Color 0,PeekByte (nums,cnt),0
    Plot i1+2,(15-i)+2
       cnt=cnt+1


Next
Next
unentropy()
;Color 0,255,0
;For i = 0 To 15
;If (PeekByte (numx,i)<>PeekByte (numz,i)) Color 255,0,0:WaitKey(): Else Color 0,255,0 
;Text 0,18+i*7,PeekByte (numx,i),False ,False
;Next


End Function